//
//  CZStatusCell.swift
//  GZWeibo009
//
//  Created by gzxzmac on 16/3/25.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

import SnapKit

/// 自定的微博cell
class CZStatusCell: UITableViewCell {
    
    // MARK: - 属性
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            // 当控制器把模型传给cell的时候调用
            // 将模型传递给CZStatusOriginalView
            originalView.status = status
            
            // 判断是否有被转的微博模型,有被转发的微博模型就显示被转发微博的view如果没有被转发的微博模型,就不显示被转发微博的view
            
            // 将这个被转发的模型赋值给被转发的view
            retweetView.retweetStatus = status?.retweeted_status
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 实现这个构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }

    private func prepareUI() {
        // 1.添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 2.添加约束
        /// 原创view
        originalView.snp_makeConstraints { (make) -> Void in
            // 当参照都一样的时候,可以简写
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
            // 原创cell的底部根据original里面的内容
//            make.height.equalTo(80 + Int(arc4random_uniform(100)))
//            make.leading.equalTo(contentView)
//            make.top.equalTo(contentView)
//            make.trailing.equalTo(contentView)
        }
        
        /// 被转发微博的View
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        /// 底部view
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
        
        // 3.当使用autolayout来约束cell里面的控件,并且cell的高度想通过autolayout来自动确定,需要给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            // contentView的左上右参照cell
            make.leading.top.trailing.equalTo(self)
            
            // contentView底部参照bottomView
            make.bottom.equalTo(bottomView)
        }
        
        // cell 的优化
        // 异步绘制
        self.layer.drawsAsynchronously = true
        
        // 栅格化/光栅化会离屏渲染
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }

    // MARK: - 懒加载
    /// 原创微博的view
    private lazy var originalView: CZStatusOriginalView = CZStatusOriginalView()
    
    /// 底部视图
    private lazy var bottomView: CZStatusBottomView = CZStatusBottomView()
    
    /// 被转发微博的View
    private lazy var retweetView: CZStatusRetweetView = CZStatusRetweetView()
}
